<template>
  <div>
    <h1>订单列表</h1>
  </div>
  <el-form :model="searchForm" :inline="true">
    <el-form-item label="请输入商品名称">
      <el-input v-model="searchForm.keyWord" clearable></el-input>
    </el-form-item>
    <el-form-item label="请输入订单id">
      <el-input v-model="searchForm.orderId" clearable></el-input>
    </el-form-item>

    <el-form-item label="订单状态" style="width: 250px">
      <el-select
        v-model="searchForm.orderStatus"
        placeholder="请选择"
        clearable
      >
        <el-option label="待支付" :value="0" />
        <el-option label="已支付" :value="1" />
        <el-option label="待发货" :value="2" />
      </el-select>
    </el-form-item>
    <el-form-item label="订单类型" style="width: 250px">
      <el-select v-model="searchForm.orderType" placeholder="请选择" clearable>
        <el-option label="立即支付" :value="1" />
        <el-option label="先用后付" :value="2" />
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="loadList">搜索</el-button>
    </el-form-item>
  </el-form>
  <div>
    <el-button type="primary" @click="toAdd()">添加订单</el-button>
    <el-button type="success" @click="toExport()">导出</el-button>
    <el-button type="danger" @click="deleteOrders()">批量删除</el-button>
  </div>

  <div>
    <el-table
      :data="tableData"
      style="width: 100%"
      ref="multipleTableRef"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" :selectable="selectable" width="55" />
      <el-table-column prop="orderId" label="订单id" width="180" />
      <el-table-column prop="orderNo" label="订单编号" width="180" />
      <el-table-column prop="orderStatus" label="订单状态" width="180">
        <template #default="scope">
          <span v-if="scope.row.orderStatus == 0">未支付</span>
          <span v-if="scope.row.orderStatus == 1">已支付</span>
          <span v-if="scope.row.orderStatus == 2">待发货</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderAmount" label="订单金额" />
      <el-table-column prop="payTime" label="购买时间" />
      <el-table-column prop="orderType" label="订单类型">
        <template #default="scope">
          <span v-if="scope.row.orderType == 1">立即支付</span>
          <span v-if="scope.row.orderType == 2">先用后付</span>
        </template>
      </el-table-column>
      <el-table-column prop="prodInfo" label="订单信息 " />

      <el-table-column prop="createBy" label="创建人 " />
      <el-table-column prop="createTime" label="创建时间 " />
      <el-table-column label="商品图片" prop="imageList">
        <template #default="scope">
          <img
            style="width: 100px; height: 100px"
            v-if="scope.row.imageList != null && scope.row.imageList.length > 0"
            v-for="item in scope.row.imageList"
            :src="item.url"
            :key="item.id"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作 ">
        <template #default="scope">
          <el-button type="success" @click="toEdit(scope.row)">编辑</el-button>
          <el-button type="info" @click="prodInfo(scope.row)"
            >商品详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>

  <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="searchForm.pageNum"
      v-model:page-size="searchForm.pageSize"
      :page-sizes="[1, 2, 3, 4]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

  <el-dialog v-model="dialogFormVisible" title="Shipping address" width="500">
    <el-form :model="saveForm" ref="ruleFormRef" :rules="rules">
      <el-form-item label="商品明细" :label-width="formLabelWidth">
        <el-input v-model="saveForm.prodInfo" autocomplete="off" />
      </el-form-item>
      <el-form-item label="订单类型" :label-width="formLabelWidth">
        <el-select v-model="saveForm.orderType" placeholder="请选择">
          <el-option label="立即付款" :value="1" />
          <el-option label="先用后付" :value="2" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">Cancel</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">
          下单
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { orderList, deleteOrder } from "@/api/api";
import { ElMessage } from "element-plus";
const tableData = ref([]);
const total = ref(0);
const searchForm = ref({
  pageSize: 4,
  pageNum: 1,
  keyWord: "",

  orderId: null,
  orderStatus: null,
  orderType: null,
  typeStatus: null,
  imageList: [],
});
const loadList = () => {
  orderList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("展示成功");
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

onMounted(() => {
  loadList();
});
import type { ComponentSize } from "element-plus";
const size = ref<ComponentSize>("default");
const background = ref(false);
const disabled = ref(false);

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};
onMounted(() => {
  loadList();
});

//添加订单
import type { FormInstance, FormRules } from "element-plus";
const rules = ref<FormRules>({
  prodInfo: [{ required: true, message: "请输入商品信息", trigger: "blur" }],
});
const dialogTableVisible = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const saveForm = ref({
  orderType: null,
  orderStatus: null,
  prodInfo: "",
});
const toAdd = () => {
  saveForm.value = {
    orderType: null,
    orderStatus: null,
    prodInfo: "",
  };
  dialogFormVisible.value = true;
};

//多选框
import type { TableInstance } from "element-plus";
import router from "@/router";
const multipleTableRef = ref<TableInstance>();
const multipleSelection = ref<[]>([]);
const handleSelectionChange = (val: any) => {
  multipleSelection.value = val;
  console.log(multipleSelection.value);
};
const selectable = (row: any) => ![1, 2].includes(row.id);
const toExport = () => {
  const ids = multipleSelection.value.map((item: any) => item.orderId);
  if (ids.length === 0) {
    ElMessage.error("id不能为空");
    return;
  }

  let a = document.createElement("a");
  a.href = "http://localhost:9011/prod/index/exportAll/" + ids;
  console.log("ids" + ids);
  a.click();
};

const deleteOrders = () => {
  deleteOrder(multipleSelection.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("删除成功");
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

const toEdit = (row: any) => {
  console.log(row.prodId);
};
</script>

<style scoped></style>
